package th.kmutt.se.is;

import th.kmutt.se.is.model.Room;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

public class RoomAdd extends Activity implements OnClickListener{
	
	Room room;
	ImageButton btnMenu1;
	Button roomStatusEmpty, roomStatusClose;
	EditText roomName, roomFloor, roomPrice, roomDepisit, roomWaterMeter, roomElectMeter;
	
	private static int roomStatusValue = 1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.screen_room_add);
		init();
	}

	private void init() {
		
		room = new Room(this);
		
		btnMenu1 = (ImageButton) findViewById(R.id.btnMenu2);
		
		roomName = (EditText) findViewById(R.id.roomName);
		roomFloor = (EditText) findViewById(R.id.roomFloor);
		roomPrice = (EditText) findViewById(R.id.roomPrice);
		roomDepisit = (EditText) findViewById(R.id.roomDeposit);
		roomWaterMeter = (EditText) findViewById(R.id.roomWaterMeter);
		roomElectMeter = (EditText) findViewById(R.id.roomElectMeter);

		roomStatusEmpty = (Button) findViewById(R.id.roomStatusEmpty);
		roomStatusClose = (Button) findViewById(R.id.roomStatusClose);
		
		

		btnMenu1.setOnClickListener(this);
		roomStatusEmpty.setOnClickListener(this);
		roomStatusClose.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
			case R.id.btnMenu2:{//Add
				if(validate()){
					
					room.setName(roomName.getText().toString().trim());
					room.setPrice(Double.parseDouble(roomPrice.getText().toString().trim()));
					room.setWaterMeter(Integer.parseInt(roomWaterMeter.getText().toString()));
					room.setElectMeter(Integer.parseInt(roomElectMeter.getText().toString()));
					room.setStatus(roomStatusValue);

					if(!roomFloor.getText().toString().trim().equals("")){
						room.setFloor(Integer.parseInt(roomFloor.getText().toString().trim()));
					}
					if(!roomDepisit.getText().toString().trim().equals("")){
						room.setDepositPrice(Double.parseDouble(roomDepisit.getText().toString().trim()));
					}
					
					if(room.create()){
						Toast.makeText(this, "Add complete", Toast.LENGTH_SHORT).show();
						finish();
					}else{
						Toast.makeText(this, "Error occur", Toast.LENGTH_SHORT).show();
					}
					
				}
			}break;
			case R.id.roomStatusEmpty:{
				
				roomStatusEmpty.setBackgroundResource(R.drawable.btn_empty_active);
				roomStatusEmpty.setTextColor(getResources().getColor(R.color.color01));
				roomStatusClose.setBackgroundResource(R.drawable.btn_close_def);
				roomStatusClose.setTextColor(getResources().getColor(R.color.color12));
				
				roomStatusValue = 1;
				
			}break;
			case R.id.roomStatusClose:{
				roomStatusEmpty.setBackgroundResource(R.drawable.btn_empty_def);
				roomStatusEmpty.setTextColor(getResources().getColor(R.color.color08));
				roomStatusClose.setBackgroundResource(R.drawable.btn_close_active);
				roomStatusClose.setTextColor(getResources().getColor(R.color.color01));

				roomStatusValue = 3;
				
			}break;
		}
	}
	
	
	private boolean validate(){
		if(roomName.getText().toString().trim().equals("")){
			Toast.makeText(this, getString(R.string.alert_msg01), Toast.LENGTH_SHORT).show();
			roomName.requestFocus();
			return false;
		}else if(roomPrice.getText().toString().trim().equals("")){
			Toast.makeText(this, getString(R.string.alert_msg02), Toast.LENGTH_SHORT).show();
			roomPrice.requestFocus();
			return false;
		}else if(roomWaterMeter.getText().toString().trim().equals("")){
			Toast.makeText(this, getString(R.string.alert_msg03), Toast.LENGTH_SHORT).show();
			roomWaterMeter.requestFocus();
			return false;
		}else if(roomElectMeter.getText().toString().trim().equals("")){
			Toast.makeText(this, getString(R.string.alert_msg04), Toast.LENGTH_SHORT).show();
			roomElectMeter.requestFocus();
			return false;
		}
		return true;
	}
}
